<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>resultutil.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_result_resultutil.js.html">resultutil.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview This file provides primitives and tools (wait, transform,
<a name="line17"></a> *     chain, combine) that make it easier to work with Results. This section
<a name="line18"></a> *     gives an overview of their functionality along with some examples and the
<a name="line19"></a> *     actual definitions have detailed descriptions next to them.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.result&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.result.DependentResult&#39;);
<a name="line27"></a>goog.require(&#39;goog.result.Result&#39;);
<a name="line28"></a>goog.require(&#39;goog.result.SimpleResult&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Returns a successful result containing the provided value.
<a name="line33"></a> *
<a name="line34"></a> * Example:
<a name="line35"></a> * &lt;pre&gt;
<a name="line36"></a> *
<a name="line37"></a> * var value = &#39;some-value&#39;;
<a name="line38"></a> * var result = goog.result.immediateResult(value);
<a name="line39"></a> * assertEquals(goog.result.Result.State.SUCCESS, result.getState());
<a name="line40"></a> * assertEquals(value, result.getValue());
<a name="line41"></a> *
<a name="line42"></a> * &lt;/pre&gt;
<a name="line43"></a> *
<a name="line44"></a> * @param {*} value The value of the result.
<a name="line45"></a> * @return {!goog.result.Result} A Result object that has already been resolved
<a name="line46"></a> *     to the supplied value.
<a name="line47"></a> */
<a name="line48"></a>goog.result.successfulResult = function(value) {
<a name="line49"></a>  var result = new goog.result.SimpleResult();
<a name="line50"></a>  result.setValue(value);
<a name="line51"></a>  return result;
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Returns a failed result with the optional error slug set.
<a name="line57"></a> *
<a name="line58"></a> * Example:
<a name="line59"></a> * &lt;pre&gt;
<a name="line60"></a> *
<a name="line61"></a> * var error = new Error(&#39;something-failed&#39;);
<a name="line62"></a> * var result = goog.result.failedResult(error);
<a name="line63"></a> * assertEquals(goog.result.Result.State.ERROR, result.getState());
<a name="line64"></a> * assertEquals(error, result.getError());
<a name="line65"></a> *
<a name="line66"></a> * &lt;/pre&gt;
<a name="line67"></a> *
<a name="line68"></a> * @param {*=} opt_error The error to which the result should resolve.
<a name="line69"></a> * @return {!goog.result.Result} A Result object that has already been resolved
<a name="line70"></a> *     to the supplied Error.
<a name="line71"></a> */
<a name="line72"></a>goog.result.failedResult = function(opt_error) {
<a name="line73"></a>  var result = new goog.result.SimpleResult();
<a name="line74"></a>  result.setError(opt_error);
<a name="line75"></a>  return result;
<a name="line76"></a>};
<a name="line77"></a>
<a name="line78"></a>
<a name="line79"></a>/**
<a name="line80"></a> * Returns a canceled result.
<a name="line81"></a> * The result will be resolved to an error of type CancelError.
<a name="line82"></a> *
<a name="line83"></a> * Example:
<a name="line84"></a> * &lt;pre&gt;
<a name="line85"></a> *
<a name="line86"></a> * var result = goog.result.canceledResult();
<a name="line87"></a> * assertEquals(goog.result.Result.State.ERROR, result.getState());
<a name="line88"></a> * var error = result.getError();
<a name="line89"></a> * assertTrue(error instanceof goog.result.Result.CancelError);
<a name="line90"></a> *
<a name="line91"></a> * &lt;/pre&gt;
<a name="line92"></a> *
<a name="line93"></a> * @return {!goog.result.Result} A canceled Result.
<a name="line94"></a> */
<a name="line95"></a>goog.result.canceledResult = function() {
<a name="line96"></a>  var result = new goog.result.SimpleResult();
<a name="line97"></a>  result.cancel();
<a name="line98"></a>  return result;
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Calls the handler on resolution of the result (success or failure).
<a name="line104"></a> * The handler is passed the result object as the only parameter. The call will
<a name="line105"></a> * be immediate if the result is no longer pending.
<a name="line106"></a> *
<a name="line107"></a> * Example:
<a name="line108"></a> * &lt;pre&gt;
<a name="line109"></a> *
<a name="line110"></a> * var result = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line111"></a> *
<a name="line112"></a> * // Wait for the result to be resolved and alert it&#39;s state.
<a name="line113"></a> * goog.result.wait(result, function(result) {
<a name="line114"></a> *   alert(&#39;State: &#39; + result.getState());
<a name="line115"></a> * });
<a name="line116"></a> * &lt;/pre&gt;
<a name="line117"></a> *
<a name="line118"></a> * @param {!goog.result.Result} result The result to install the handlers.
<a name="line119"></a> * @param {!function(this:T, !goog.result.Result)} handler The handler to be
<a name="line120"></a> *     called. The handler is passed the result object as the only parameter.
<a name="line121"></a> * @param {!T=} opt_scope Optional scope for the handler.
<a name="line122"></a> * @template T
<a name="line123"></a> */
<a name="line124"></a>goog.result.wait = function(result, handler, opt_scope) {
<a name="line125"></a>  result.wait(opt_scope ? goog.bind(handler, opt_scope) : handler);
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Calls the handler if the result succeeds. The result object is the only
<a name="line131"></a> * parameter passed to the handler. The call will be immediate if the result
<a name="line132"></a> * has already succeeded.
<a name="line133"></a> *
<a name="line134"></a> * Example:
<a name="line135"></a> * &lt;pre&gt;
<a name="line136"></a> *
<a name="line137"></a> * var result = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line138"></a> *
<a name="line139"></a> * // attach a success handler.
<a name="line140"></a> * goog.result.waitOnSuccess(result, function(result) {
<a name="line141"></a> *   var datavalue = result.getvalue();
<a name="line142"></a> *   alert(&#39;value : &#39; + datavalue);
<a name="line143"></a> * });
<a name="line144"></a> * &lt;/pre&gt;
<a name="line145"></a> *
<a name="line146"></a> * @param {!goog.result.Result} result The result to install the handlers.
<a name="line147"></a> * @param {!function(this:T, ?, !goog.result.Result)} handler The handler to be
<a name="line148"></a> *     called. The handler is passed the result value and the result as
<a name="line149"></a> *     parameters.
<a name="line150"></a> * @param {!T=} opt_scope Optional scope for the handler.
<a name="line151"></a> * @template T
<a name="line152"></a> */
<a name="line153"></a>goog.result.waitOnSuccess = function(result, handler, opt_scope) {
<a name="line154"></a>  goog.result.wait(result, function(res) {
<a name="line155"></a>    if (res.getState() == goog.result.Result.State.SUCCESS) {
<a name="line156"></a>      // &#39;this&#39; refers to opt_scope
<a name="line157"></a>      handler.call(this, res.getValue(), res);
<a name="line158"></a>    }
<a name="line159"></a>  }, opt_scope);
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Calls the handler if the result action errors. The result object is passed as
<a name="line165"></a> * the only parameter to the handler. The call will be immediate if the result
<a name="line166"></a> * object has already resolved to an error.
<a name="line167"></a> *
<a name="line168"></a> * Example:
<a name="line169"></a> *
<a name="line170"></a> * &lt;pre&gt;
<a name="line171"></a> *
<a name="line172"></a> * var result = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line173"></a> *
<a name="line174"></a> * // Attach a failure handler.
<a name="line175"></a> * goog.result.waitOnError(result, function(error) {
<a name="line176"></a> *  // Failed asynchronous call!
<a name="line177"></a> * });
<a name="line178"></a> * &lt;/pre&gt;
<a name="line179"></a> *
<a name="line180"></a> * @param {!goog.result.Result} result The result to install the handlers.
<a name="line181"></a> * @param {!function(this:T, !goog.result.Result)} handler The handler to be
<a name="line182"></a> *     called. The handler is passed the result object as the only parameter.
<a name="line183"></a> * @param {!T=} opt_scope Optional scope for the handler.
<a name="line184"></a> * @template T
<a name="line185"></a> */
<a name="line186"></a>goog.result.waitOnError = function(result, handler, opt_scope) {
<a name="line187"></a>  goog.result.wait(result, function(res) {
<a name="line188"></a>    if (res.getState() == goog.result.Result.State.ERROR) {
<a name="line189"></a>      // &#39;this&#39; refers to opt_scope
<a name="line190"></a>      handler.call(this, res);
<a name="line191"></a>    }
<a name="line192"></a>  }, opt_scope);
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Given a result and a transform function, returns a new result whose value,
<a name="line198"></a> * on success, will be the value of the given result after having been passed
<a name="line199"></a> * through the transform function.
<a name="line200"></a> *
<a name="line201"></a> * If the given result is an error, the returned result is also an error and the
<a name="line202"></a> * transform will not be called.
<a name="line203"></a> *
<a name="line204"></a> * Example:
<a name="line205"></a> * &lt;pre&gt;
<a name="line206"></a> *
<a name="line207"></a> * var result = xhr.getJson(&#39;testdata/xhr_test_json.data&#39;);
<a name="line208"></a> *
<a name="line209"></a> * // Transform contents of returned data using &#39;processJson&#39; and create a
<a name="line210"></a> * // transformed result to use returned JSON.
<a name="line211"></a> * var transformedResult = goog.result.transform(result, processJson);
<a name="line212"></a> *
<a name="line213"></a> * // Attach success and failure handlers to the tranformed result.
<a name="line214"></a> * goog.result.waitOnSuccess(transformedResult, function(result) {
<a name="line215"></a> *   var jsonData = result.getValue();
<a name="line216"></a> *   assertEquals(&#39;ok&#39;, jsonData[&#39;stat&#39;]);
<a name="line217"></a> * });
<a name="line218"></a> *
<a name="line219"></a> * goog.result.waitOnError(transformedResult, function(error) {
<a name="line220"></a> *   // Failed getJson call
<a name="line221"></a> * });
<a name="line222"></a> * &lt;/pre&gt;
<a name="line223"></a> *
<a name="line224"></a> * @param {!goog.result.Result} result The result whose value will be
<a name="line225"></a> *     transformed.
<a name="line226"></a> * @param {!function(?):?} transformer The transformer
<a name="line227"></a> *     function. The return value of this function will become the value of the
<a name="line228"></a> *     returned result.
<a name="line229"></a> *
<a name="line230"></a> * @return {!goog.result.DependentResult} A new Result whose eventual value will
<a name="line231"></a> *     be the returned value of the transformer function.
<a name="line232"></a> */
<a name="line233"></a>goog.result.transform = function(result, transformer) {
<a name="line234"></a>  var returnedResult = new goog.result.DependentResultImpl_([result]);
<a name="line235"></a>
<a name="line236"></a>  goog.result.wait(result, function(res) {
<a name="line237"></a>    if (res.getState() == goog.result.Result.State.SUCCESS) {
<a name="line238"></a>      returnedResult.setValue(transformer(res.getValue()));
<a name="line239"></a>    } else {
<a name="line240"></a>      returnedResult.setError(res.getError());
<a name="line241"></a>    }
<a name="line242"></a>  });
<a name="line243"></a>
<a name="line244"></a>  return returnedResult;
<a name="line245"></a>};
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/**
<a name="line249"></a> * The chain function aids in chaining of asynchronous Results. This provides a
<a name="line250"></a> * convenience for use cases where asynchronous operations must happen serially
<a name="line251"></a> * i.e. subsequent asynchronous operations are dependent on data returned by
<a name="line252"></a> * prior asynchronous operations.
<a name="line253"></a> *
<a name="line254"></a> * It accepts a result and an action callback as arguments and returns a
<a name="line255"></a> * result. The action callback is called when the first result succeeds and is
<a name="line256"></a> * supposed to return a second result. The returned result is resolved when one
<a name="line257"></a> * of both of the results resolve (depending on their success or failure.) The
<a name="line258"></a> * state and value of the returned result in the various cases is documented
<a name="line259"></a> * below:
<a name="line260"></a> *
<a name="line261"></a> * First Result State:    Second Result State:    Returned Result State:
<a name="line262"></a> * SUCCESS                SUCCESS                 SUCCESS
<a name="line263"></a> * SUCCESS                ERROR                   ERROR
<a name="line264"></a> * ERROR                  Not created             ERROR
<a name="line265"></a> *
<a name="line266"></a> * The value of the returned result, in the case both results succeed, is the
<a name="line267"></a> * value of the second result (the result returned by the action callback.)
<a name="line268"></a> *
<a name="line269"></a> * Example:
<a name="line270"></a> * &lt;pre&gt;
<a name="line271"></a> *
<a name="line272"></a> * var testDataResult = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line273"></a> *
<a name="line274"></a> * // Chain this result to perform another asynchronous operation when this
<a name="line275"></a> * // Result is resolved.
<a name="line276"></a> * var chainedResult = goog.result.chain(testDataResult,
<a name="line277"></a> *     function(testDataResult) {
<a name="line278"></a> *
<a name="line279"></a> *       // The result value of testDataResult is the URL for JSON data.
<a name="line280"></a> *       var jsonDataUrl = testDataResult.getValue();
<a name="line281"></a> *
<a name="line282"></a> *       // Create a new Result object when the original result is resolved.
<a name="line283"></a> *       var jsonResult = xhr.getJson(jsonDataUrl);
<a name="line284"></a> *
<a name="line285"></a> *       // Return the newly created Result.
<a name="line286"></a> *       return jsonResult;
<a name="line287"></a> *     });
<a name="line288"></a> *
<a name="line289"></a> * // The chained result resolves to success when both results resolve to
<a name="line290"></a> * // success.
<a name="line291"></a> * goog.result.waitOnSuccess(chainedResult, function(result) {
<a name="line292"></a> *
<a name="line293"></a> *   // At this point, both results have succeeded and we can use the JSON
<a name="line294"></a> *   // data returned by the second asynchronous call.
<a name="line295"></a> *   var jsonData = result.getValue();
<a name="line296"></a> *   assertEquals(&#39;ok&#39;, jsonData[&#39;stat&#39;]);
<a name="line297"></a> * });
<a name="line298"></a> *
<a name="line299"></a> * // Attach the error handler to be called when either Result fails.
<a name="line300"></a> * goog.result.waitOnError(chainedResult, function(result) {
<a name="line301"></a> *   alert(&#39;chained result failed!&#39;);
<a name="line302"></a> * });
<a name="line303"></a> * &lt;/pre&gt;
<a name="line304"></a> *
<a name="line305"></a> * @param {!goog.result.Result} result The result to chain.
<a name="line306"></a> * @param {!function(!goog.result.Result):!goog.result.Result}
<a name="line307"></a> *     actionCallback The callback called when the result is resolved. This
<a name="line308"></a> *     callback must return a Result.
<a name="line309"></a> *
<a name="line310"></a> * @return {!goog.result.DependentResult} A result that is resolved when both
<a name="line311"></a> *     the given Result and the Result returned by the actionCallback have
<a name="line312"></a> *     resolved.
<a name="line313"></a> */
<a name="line314"></a>goog.result.chain = function(result, actionCallback) {
<a name="line315"></a>  var dependentResult = new goog.result.DependentResultImpl_([result]);
<a name="line316"></a>
<a name="line317"></a>  // Wait for the first action.
<a name="line318"></a>  goog.result.wait(result, function(result) {
<a name="line319"></a>    if (result.getState() == goog.result.Result.State.SUCCESS) {
<a name="line320"></a>
<a name="line321"></a>      // The first action succeeded. Chain the contingent action.
<a name="line322"></a>      var contingentResult = actionCallback(result);
<a name="line323"></a>      dependentResult.addParentResult(contingentResult);
<a name="line324"></a>      goog.result.wait(contingentResult, function(contingentResult) {
<a name="line325"></a>
<a name="line326"></a>        // The contingent action completed. Set the dependent result based on
<a name="line327"></a>        // the contingent action&#39;s outcome.
<a name="line328"></a>        if (contingentResult.getState() ==
<a name="line329"></a>            goog.result.Result.State.SUCCESS) {
<a name="line330"></a>          dependentResult.setValue(contingentResult.getValue());
<a name="line331"></a>        } else {
<a name="line332"></a>          dependentResult.setError(contingentResult.getError());
<a name="line333"></a>        }
<a name="line334"></a>      });
<a name="line335"></a>    } else {
<a name="line336"></a>      // First action failed, the dependent result should also fail.
<a name="line337"></a>      dependentResult.setError(result.getError());
<a name="line338"></a>    }
<a name="line339"></a>  });
<a name="line340"></a>
<a name="line341"></a>  return dependentResult;
<a name="line342"></a>};
<a name="line343"></a>
<a name="line344"></a>
<a name="line345"></a>/**
<a name="line346"></a> * Returns a result that waits on all given results to resolve. Once all have
<a name="line347"></a> * resolved, the returned result will succeed (and never error).
<a name="line348"></a> *
<a name="line349"></a> * Example:
<a name="line350"></a> * &lt;pre&gt;
<a name="line351"></a> *
<a name="line352"></a> * var result1 = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line353"></a> *
<a name="line354"></a> * // Get a second independent Result.
<a name="line355"></a> * var result2 = xhr.getJson(&#39;testdata/xhr_test_json.data&#39;);
<a name="line356"></a> *
<a name="line357"></a> * // Create a Result that resolves when both prior results resolve.
<a name="line358"></a> * var combinedResult = goog.result.combine(result1, result2);
<a name="line359"></a> *
<a name="line360"></a> * // Process data after resolution of both results.
<a name="line361"></a> * goog.result.waitOnSuccess(combinedResult, function(results) {
<a name="line362"></a> *   goog.array.forEach(results, function(result) {
<a name="line363"></a> *       alert(result.getState());
<a name="line364"></a> *   });
<a name="line365"></a> * });
<a name="line366"></a> * &lt;/pre&gt;
<a name="line367"></a> *
<a name="line368"></a> * @param {...!goog.result.Result} var_args The results to wait on.
<a name="line369"></a> *
<a name="line370"></a> * @return {!goog.result.DependentResult} A new Result whose eventual value will
<a name="line371"></a> *     be the resolved given Result objects.
<a name="line372"></a> */
<a name="line373"></a>goog.result.combine = function(var_args) {
<a name="line374"></a>  /** @type {!Array.&lt;!goog.result.Result&gt;} */
<a name="line375"></a>  var results = goog.array.clone(arguments);
<a name="line376"></a>  var combinedResult = new goog.result.DependentResultImpl_(results);
<a name="line377"></a>
<a name="line378"></a>  var isResolved = function(res) {
<a name="line379"></a>    return res.getState() != goog.result.Result.State.PENDING;
<a name="line380"></a>  };
<a name="line381"></a>
<a name="line382"></a>  var checkResults = function() {
<a name="line383"></a>    if (combinedResult.getState() == goog.result.Result.State.PENDING &amp;&amp;
<a name="line384"></a>        goog.array.every(results, isResolved)) {
<a name="line385"></a>      combinedResult.setValue(results);
<a name="line386"></a>    }
<a name="line387"></a>  };
<a name="line388"></a>
<a name="line389"></a>  goog.array.forEach(results, function(result) {
<a name="line390"></a>    goog.result.wait(result, checkResults);
<a name="line391"></a>  });
<a name="line392"></a>
<a name="line393"></a>  return combinedResult;
<a name="line394"></a>};
<a name="line395"></a>
<a name="line396"></a>
<a name="line397"></a>/**
<a name="line398"></a> * Returns a result that waits on all given results to resolve. Once all have
<a name="line399"></a> * resolved, the returned result will succeed if and only if all given results
<a name="line400"></a> * succeeded. Otherwise it will error.
<a name="line401"></a> *
<a name="line402"></a> * Example:
<a name="line403"></a> * &lt;pre&gt;
<a name="line404"></a> *
<a name="line405"></a> * var result1 = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line406"></a> *
<a name="line407"></a> * // Get a second independent Result.
<a name="line408"></a> * var result2 = xhr.getJson(&#39;testdata/xhr_test_json.data&#39;);
<a name="line409"></a> *
<a name="line410"></a> * // Create a Result that resolves when both prior results resolve.
<a name="line411"></a> * var combinedResult = goog.result.combineOnSuccess(result1, result2);
<a name="line412"></a> *
<a name="line413"></a> * // Process data after successful resolution of both results.
<a name="line414"></a> * goog.result.waitOnSuccess(combinedResult, function(results) {
<a name="line415"></a> *   var textData = results[0].getValue();
<a name="line416"></a> *   var jsonData = results[1].getValue();
<a name="line417"></a> *   assertEquals(&#39;Just some data.&#39;, textData);
<a name="line418"></a> *   assertEquals(&#39;ok&#39;, jsonData[&#39;stat&#39;]);
<a name="line419"></a> * });
<a name="line420"></a> *
<a name="line421"></a> * // Handle errors when either or both results failed.
<a name="line422"></a> * goog.result.waitOnError(combinedResult, function(combined) {
<a name="line423"></a> *   var results = combined.getError();
<a name="line424"></a> *
<a name="line425"></a> *   if (results[0].getState() == goog.result.Result.State.ERROR) {
<a name="line426"></a> *     alert(&#39;result1 failed&#39;);
<a name="line427"></a> *   }
<a name="line428"></a> *
<a name="line429"></a> *   if (results[1].getState() == goog.result.Result.State.ERROR) {
<a name="line430"></a> *     alert(&#39;result2 failed&#39;);
<a name="line431"></a> *   }
<a name="line432"></a> * });
<a name="line433"></a> * &lt;/pre&gt;
<a name="line434"></a> *
<a name="line435"></a> * @param {...!goog.result.Result} var_args The results to wait on.
<a name="line436"></a> *
<a name="line437"></a> * @return {!goog.result.DependentResult} A new Result whose eventual value will
<a name="line438"></a> *     be an array of values of the given Result objects.
<a name="line439"></a> */
<a name="line440"></a>goog.result.combineOnSuccess = function(var_args) {
<a name="line441"></a>  var results = goog.array.clone(arguments);
<a name="line442"></a>  var combinedResult = new goog.result.DependentResultImpl_(results);
<a name="line443"></a>
<a name="line444"></a>  var resolvedSuccessfully = function(res) {
<a name="line445"></a>    return res.getState() == goog.result.Result.State.SUCCESS;
<a name="line446"></a>  };
<a name="line447"></a>
<a name="line448"></a>  goog.result.wait(
<a name="line449"></a>      goog.result.combine.apply(goog.result.combine, results),
<a name="line450"></a>      // The combined result never ERRORs
<a name="line451"></a>      function(res) {
<a name="line452"></a>        var results = /** @type {Array.&lt;!goog.result.Result&gt;} */ (
<a name="line453"></a>            res.getValue());
<a name="line454"></a>        if (goog.array.every(results, resolvedSuccessfully)) {
<a name="line455"></a>          combinedResult.setValue(results);
<a name="line456"></a>        } else {
<a name="line457"></a>          combinedResult.setError(results);
<a name="line458"></a>        }
<a name="line459"></a>      });
<a name="line460"></a>
<a name="line461"></a>  return combinedResult;
<a name="line462"></a>};
<a name="line463"></a>
<a name="line464"></a>
<a name="line465"></a>/**
<a name="line466"></a> * Given a DependentResult, cancels the Results it depends on (that is, the
<a name="line467"></a> * results returned by getParentResults). This function does not recurse,
<a name="line468"></a> * so e.g. parents of parents are not canceled; only the immediate parents of
<a name="line469"></a> * the given Result are canceled.
<a name="line470"></a> *
<a name="line471"></a> * Example using @see goog.result.combine:
<a name="line472"></a> * &lt;pre&gt;
<a name="line473"></a> * var result1 = xhr.get(&#39;testdata/xhr_test_text.data&#39;);
<a name="line474"></a> *
<a name="line475"></a> * // Get a second independent Result.
<a name="line476"></a> * var result2 = xhr.getJson(&#39;testdata/xhr_test_json.data&#39;);
<a name="line477"></a> *
<a name="line478"></a> * // Create a Result that resolves when both prior results resolve.
<a name="line479"></a> * var combinedResult = goog.result.combineOnSuccess(result1, result2);
<a name="line480"></a> *
<a name="line481"></a> * combinedResult.wait(function() {
<a name="line482"></a> *   if (combinedResult.isCanceled()) {
<a name="line483"></a> *     goog.result.cancelParentResults(combinedResult);
<a name="line484"></a> *   }
<a name="line485"></a> * });
<a name="line486"></a> *
<a name="line487"></a> * // Now, canceling combinedResult will cancel both result1 and result2.
<a name="line488"></a> * combinedResult.cancel();
<a name="line489"></a> * &lt;/pre&gt;
<a name="line490"></a> * @param {!goog.result.DependentResult} dependentResult A Result that is
<a name="line491"></a> *     dependent on the values of other Results (for example the Result of a
<a name="line492"></a> *     goog.result.combine, goog.result.chain, or goog.result.transform call).
<a name="line493"></a> * @return {boolean} True if any results were successfully canceled; otherwise
<a name="line494"></a> *     false.
<a name="line495"></a> * TODO(user): Implement a recursive version of this that cancels all
<a name="line496"></a> * ancestor results.
<a name="line497"></a> */
<a name="line498"></a>goog.result.cancelParentResults = function(dependentResult) {
<a name="line499"></a>  var anyCanceled = false;
<a name="line500"></a>  goog.array.forEach(dependentResult.getParentResults(), function(result) {
<a name="line501"></a>    anyCanceled |= result.cancel();
<a name="line502"></a>  });
<a name="line503"></a>  return !!anyCanceled;
<a name="line504"></a>};
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>
<a name="line508"></a>/**
<a name="line509"></a> * A DependentResult represents a Result whose eventual value depends on the
<a name="line510"></a> * value of one or more other Results. For example, the Result returned by
<a name="line511"></a> * @see goog.result.chain or @see goog.result.combine is dependent on the
<a name="line512"></a> * Results given as arguments.
<a name="line513"></a> *
<a name="line514"></a> * @param {!Array.&lt;!goog.result.Result&gt;} parentResults A list of Results that
<a name="line515"></a> *     will affect the eventual value of this Result.
<a name="line516"></a> * @constructor
<a name="line517"></a> * @implements {goog.result.DependentResult}
<a name="line518"></a> * @extends {goog.result.SimpleResult}
<a name="line519"></a> * @private
<a name="line520"></a> */
<a name="line521"></a>goog.result.DependentResultImpl_ = function(parentResults) {
<a name="line522"></a>  goog.base(this);
<a name="line523"></a>  /**
<a name="line524"></a>   * A list of Results that will affect the eventual value of this Result.
<a name="line525"></a>   * @type {!Array.&lt;!goog.result.Result&gt;}
<a name="line526"></a>   * @private
<a name="line527"></a>   */
<a name="line528"></a>  this.parentResults_ = parentResults;
<a name="line529"></a>};
<a name="line530"></a>goog.inherits(goog.result.DependentResultImpl_, goog.result.SimpleResult);
<a name="line531"></a>
<a name="line532"></a>
<a name="line533"></a>/**
<a name="line534"></a> * Adds a Result to the list of Results that affect this one.
<a name="line535"></a> * @param {!goog.result.Result} parentResult A result whose value affects the
<a name="line536"></a> *     value of this Result.
<a name="line537"></a> */
<a name="line538"></a>goog.result.DependentResultImpl_.prototype.addParentResult = function(
<a name="line539"></a>    parentResult) {
<a name="line540"></a>  this.parentResults_.push(parentResult);
<a name="line541"></a>};
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>/** @override */
<a name="line545"></a>goog.result.DependentResultImpl_.prototype.getParentResults = function() {
<a name="line546"></a>  return this.parentResults_;
<a name="line547"></a>};
</pre>


</body>
</html>
